import pymysql

import datetime
import time

# 日期时间字符串
st = "2017-11-23 16:10:10"
# 当前日期时间
dt = datetime.datetime.now()
# 当前时间戳
sp = time.time()
# insert into path(path_ID,start_position,end_position,start_time,total_time,trailer_ID) values("20211219104131trailer001",1,1,"2021-12-19 10:41:31","00:12:13","traler001");
# str = '20211219104131trailer001'


# analysisPathID(str)
# print(analysisPathIDToTimeType(str))


# 0.获得当前datetime字符串
def cur_datetime_String():
    return datetime.datetime.now().strftime()


# 1.把datetime转成字符串
def datetime_toString(dt):
    print("1.把datetime转成字符串: ", dt.strftime("%Y-%m-%d %H:%M:%S"))
    return dt.strftime("%Y%m%d%H%M%S")


# 2.把字符串转成datetime
def string_toDatetime(st):
    print("2.把字符串转成datetime: ", datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S"))
    return datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S")


# 3.把字符串转成时间戳形式
def string_toTimestamp(st):
    print("3.把字符串转成时间戳形式:", time.mktime(time.strptime(st, "%Y-%m-%d %H:%M:%S")))


# 4.把时间戳转成字符串形式
def timestamp_toString(sp):
    print("4.把时间戳转成字符串形式: ", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sp)))
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sp))


# 5.把datetime类型转外时间戳形式
def datetime_toTimestamp(dt):
    print("5.把datetime类型转外时间戳形式:", time.mktime(dt.timetuple()))
    return time.mktime(dt.timetuple())


def getDbConnection():
    HOST = '127.0.0.1'
    PORT = 3306
    DATABASE = 'Trailer_management'
    USERNAME = 'root'
    PASSWORD = '123456'
    return pymysql.connect(host=HOST, user=USERNAME, password=PASSWORD, database=DATABASE, port=PORT)
